home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / xlib / xlib06p2 / xpolygon.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-09  |  3.1 KB  |  137 lines

  1. #include "xinternl.h"
  2. #include "dipoly.h"
  3. #include <conio.h>
  4. #include <mem.h>
  5.  
  6. /*==================================================================
  7. XPOLYGON.CPP contains the code for polygon graphics using the
  8. device-independent polygon scan routines from Michael Abrash's
  9. Doctor Dobbs columns
  10. ===================================================================*/
  11.  
  12. extern BYTE * pbVGABuffer;
  13. extern int ScrnLogicalByteWidth;
  14. extern BYTE abLeftClipPlaneMask[];
  15. extern BYTE abRightClipPlaneMask[];
  16. extern int TopClip;
  17. extern int BottomClip;
  18.  
  19. struct VERTEX { 
  20.   int X; 
  21.   int Y;
  22. };
  23.  
  24. inline void _HorizontalLine(
  25.     xPageHandle_t wOffset,
  26.     xScreenCoord_t iLeftX,
  27.     xScreenCoord_t iY,
  28.     int iLength,
  29.     xColor_t iColor
  30. )
  31. {
  32.   if ( iLength < 0 ) {
  33.     return;
  34.   }
  35.   BYTE * pb = pbVGABuffer + wOffset + ( ScrnLogicalByteWidth * iY ) + iLeftX / 4;
  36.   int iTemp;
  37.   //do the left part of the line
  38.   if ( iTemp = ( iLeftX & 3 ) ) {
  39.     outp( SC_INDEX, 0x02 );
  40.     outp( SC_INDEX + 1, abLeftClipPlaneMask[ iTemp ] );
  41.     *pb++ = ( BYTE )iColor;
  42.     iLength -= ( 4 - iTemp );
  43.   }
  44.   if ( iLength < 0 ) {
  45.     return;
  46.   }
  47.   //the middle
  48.   if ( iTemp = ( iLength >> 2 ) ) {
  49.     outpw( SC_INDEX, 0x0f02 );
  50.     iLength &= 3;
  51.     memset( pb, iColor, iTemp );
  52.     pb += iTemp;
  53.   }
  54.   //then the right part
  55.   if ( iLength ) {
  56.     outp( SC_INDEX, 0x02 );
  57.     outp( SC_INDEX + 1, abRightClipPlaneMask[ iLength ] );
  58.     *pb = ( BYTE )iColor;
  59.   }
  60. }
  61.  
  62.  
  63. /*
  64. _DrawHLineList() draws a list of horizontal lines passed by the scanning
  65. routine.
  66. */
  67. void _DrawHLineList(
  68.   xPageHandle_t pagPage,
  69.   hLineList * phll,
  70.   xColor_t colColor
  71. )
  72. {
  73.   //clip our y coordinates
  74.   int iStartLine = 0;
  75.   int iLinesToDraw = phll->iLength;
  76.   //clip top
  77.   if ( TopClip > phll->iYStart ) {
  78.     iStartLine = TopClip - phll->iYStart;
  79.     iLinesToDraw -= iStartLine;
  80.   }
  81.   //clip bottom
  82.   int iBottomLine = phll->iYStart + phll->iLength;
  83.   if ( BottomClip < iBottomLine ) {
  84.     iLinesToDraw -= ( iBottomLine - BottomClip );
  85.   }
  86.   //now draw our list
  87.   hLine * phln = phll->phln;
  88.   int iLineCounter = phll->iYStart;
  89.   while ( iLinesToDraw-- ) {
  90.     _HorizontalLine( pagPage, phln->iXStart, iLineCounter++, phln->iXEnd - phln->iXStart, colColor );
  91.     ++phln;
  92.   }
  93. }
  94.  
  95. void x_triangle(     /* Draw a triangle */
  96.   int x0,
  97.   int y0,
  98.   int x1,
  99.   int y1,
  100.   int x2,
  101.   int y2,
  102.   xColor_t colColor,
  103.   xPageHandle_t pagPage
  104. )
  105. {
  106.   vertexListHeader vtl;
  107.     vtl.iLength = 3;
  108.   vertex avtx[ 3 ] = { { x0, y0 }, { x1, y1 }, { x2, y2 } };
  109.   vtl.pvtx = avtx;
  110.   hLineList * phll = phllScannedPolygon( &vtl, 0, 0 );
  111.   if ( phll ) {
  112.       _DrawHLineList( pagPage, phll, colColor );
  113.     if ( phll->phln ) {
  114.         delete( phll->phln );
  115.     }
  116.   }
  117. }
  118.  
  119. void x_polygon(     /* Draw a convex polygon */
  120.   VERTEX * pvertices,
  121.   int  num_vertices,
  122.   xColor_t colColor,
  123.   xPageHandle_t pagPage
  124. )
  125. {
  126.   vertexListHeader vtl;
  127.   vtl.iLength = num_vertices;
  128.   vtl.pvtx = ( vertex * )pvertices;
  129.   hLineList * phll = phllScannedPolygon( &vtl, 0, 0 );
  130.   if ( phll ) {
  131.     _DrawHLineList( pagPage, phll, colColor );
  132.     if ( phll->phln ) {
  133.         delete( phll->phln );
  134.     }
  135.   }
  136. }
  137.